package com.hiwan.system.service.dept;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.system.cache.MyCacheService;
import com.hiwan.system.cache.SysCacheService;
import com.hiwan.system.dao.dept.DeptDao;
import com.hiwan.system.pojo.DeptInfo;
import com.hiwan.system.pojo.ParameterInfo;

@Service
@Transactional
public class DeptService extends MyCacheService<DeptInfo>{
	
	
	@Autowired
	private DeptDao deptDao;
	@Autowired
	private SysCacheService sysCacheService;

	public DeptService() {
		super("deptCache");
	}

	// 查询部门信息
	// 查询部门信息
	public DeptInfo getDeptInfo(String deptId) {
		DeptInfo deptInfo = null;
		/*try {
			deptInfo = (DeptInfo) this.getCacheService().get("deptCache",
					deptId);
		} catch (NoSuchFieldException e) {*/
			deptInfo = (DeptInfo) deptDao.selectDeptInfo(deptId);
			if (deptInfo != null) {
				this.getCacheService().put(this.getCacheNameSpace(), deptId,
						deptInfo);
			}else{
				deptInfo = new DeptInfo();
			}

		/*}*/
		return deptInfo;
	}

	// 查询部门列表
	public List<DeptInfo> getDeptList(String qOrg, String qDept, String qType) {
		List<DeptInfo> deptList = null;
		deptList = (List<DeptInfo>) deptDao.selectDeptList(qOrg, qDept, qType);
		return deptList;
	}

	public String getDeptName(String deptId) {
		DeptInfo deptInfo = getDeptInfo(deptId);
		if (deptInfo != null) {
			return deptInfo.getDeptName();
		} else {
			return "";
		}
	}

	/**
	 * 取得缓存中是否包含部门的参数
	 */

	public boolean hasDept() {
		ParameterInfo s = sysCacheService.getSysParameter("dept_yn", "*",
				"*");
		if (s == null) {
			return true;
		}
		return StringUtils.equals(s.getParam1(), "yes");
	}

	/**
	 * 根据belog_org和root_dept查询部门信息
	 * 
	 * @param deptInfo
	 * @return
	 */
	public List<DeptInfo> selectSubDeptList(DeptInfo deptInfo) {
		return deptDao.selectSubDeptList(deptInfo);
	}

	@Override
	protected int insert(DeptInfo deptInfo) throws SQLException {
		return deptDao.insertDeptInfoByMapper(deptInfo);
	}

	@Override
	protected int modify(DeptInfo deptInfo) throws SQLException {
		return deptDao.updateDeptInfoByMapper(deptInfo);
	}

	@Override
	public int delete(DeptInfo deptInfo) throws SQLException {
		return deptDao.deleteDeptInfoByMapper(deptInfo);
	}

	@Override
	protected Serializable getKey(DeptInfo entry) {
		return entry.getDeptId();
	}
	@Override
	public void insertEntry(DeptInfo entry) throws SQLException{
		super.insertEntry(entry);
		reflashParentOrgCache(entry);
	}
	@Override
	public void modifyEntry(DeptInfo entry) throws SQLException{
		super.modifyEntry(entry);
		reflashParentOrgCache(entry);
		
	}
	@Override
	public void deleteEntry(DeptInfo entry) throws SQLException{
		super.deleteEntry(entry);
		reflashParentOrgCache(entry);
	}
	
	private void reflashParentOrgCache(DeptInfo entry){
		super.getCacheService().remove("sysCache", entry.getBelongOrg());
	}
	
	public List<DeptInfo> getDeptByName(String deptName,String parentDept,String belongOrg){
		return deptDao.selectDeptByName(deptName, parentDept, belongOrg);
	}
	
	public int updateDeptFromXXY(DeptInfo deptInfo){
		return deptDao.updateDeptFromXXY(deptInfo);
	}
	
	public int insertDeptFromXXY(DeptInfo deptInfo){
		return this.insertDeptFromXXY(deptInfo);
	}
}